package com.jams.goodox.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jams.goodox.R;
import com.jams.goodox.activity.wallet.PayConstants;
import com.jams.goodox.adapter.RechargeBackRedPackageAdapter;
import com.jams.goodox.entity.RechargeRedPacketsInfo;
import com.jams.goodox.listen.OnPasswordInputFinish;
import com.jams.goodox.widget.PasswordView;
import com.jams.goodox.widget.WheelView;

import java.util.List;

/**
 * Created by jams on 2017/5/2.
 * ***
 * Tel: ***
 */

public class MessageDialog {
    /*
      *选择支付方式的底部的dialog
      */
    public static Dialog showPaymentBottomMenu(Context context, List<Integer> list, final MenuDialogOnItemClickListener menuDialogClickListener) {
        return ShowPaymentBottomMenuDialog(context, list, menuDialogClickListener);
    }

    private static Dialog ShowPaymentBottomMenuDialog(Context context, List<Integer> list, final MenuDialogOnItemClickListener menuDialogClickListener) {
        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_select_payment3, null);
        TextView tvAliPay = (TextView) view.findViewById(R.id.tv_alipay);
        TextView tvWx = (TextView) view.findViewById(R.id.tv_wx);
        TextView tvYl = (TextView) view.findViewById(R.id.tv_yl);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        tvAliPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(PayConstants.ZFB);
                dialog.dismiss();
            }
        });
        tvWx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(PayConstants.WX);
                dialog.dismiss();
            }
        });
        tvYl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(PayConstants.YL);
                dialog.dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        view.findViewById(R.id.layout1).setVisibility(list.contains(PayConstants.ZFB) ? View.VISIBLE : View.GONE);
        view.findViewById(R.id.layout2).setVisibility(list.contains(PayConstants.WX) ? View.VISIBLE : View.GONE);
        view.findViewById(R.id.layout3).setVisibility(list.contains(PayConstants.YL) ? View.VISIBLE : View.GONE);

        dialog.setContentView(view);
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context);
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context);
        }
        mWindow.setAttributes(lp);
        mWindow.setGravity(Gravity.BOTTOM);
        mWindow.setWindowAnimations(R.style.Menu_PopuWindow_style);
        dialog.show();
        return dialog;
    }

    public interface MenuDialogOnItemClickListener {
        void onItemClick(int index);
    }

    /*
       *选择支付方式的底部的dialog
       */
    public static Dialog showRedPackageList(Context context, List<RechargeRedPacketsInfo.DataBean> packetInfos, final MenuDialogOnItemClickListener menuDialogClickListener) {
        return showRedPackageDialog(context, packetInfos, menuDialogClickListener);
    }

    private static Dialog showRedPackageDialog(Context context, List<RechargeRedPacketsInfo.DataBean> packetInfos, final MenuDialogOnItemClickListener listener) {
        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.red_package_dialog, null);
        dialog.setContentView(view);
        RechargeBackRedPackageAdapter adapter = new RechargeBackRedPackageAdapter(context, packetInfos);
        ListView listView = (ListView) view.findViewById(R.id.listview_red_package);
        TextView tvTipsTitle = (TextView) view.findViewById(R.id.tv_tips_dialog_title);
        tvTipsTitle.setText(String.format("充值成功\n恭喜您获得%d个红包", packetInfos.size()));
        LinearLayout close = (LinearLayout) view.findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (null != listener)
                    listener.onItemClick(0);
            }
        });
        listView.setAdapter(adapter);
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context) * 8 / 9;
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context) * 8 / 9;
        }
        mWindow.setAttributes(lp);
        mWindow.setGravity(Gravity.CENTER);
        dialog.show();
        return dialog;
    }

    /*
      *只有知道了单个确认提示对话框（没取消按钮的）
      */
    public static Dialog showSingleConfirmDialog(Context context, String title, String content, String sureText, final DialogConfirmClickListener dialogConfirmClickListener) {
        return ShowSingleConfirmDialog(context, title, content, sureText, dialogConfirmClickListener);
    }

    private static Dialog ShowSingleConfirmDialog(Context context, String title, String toast, String sureText, final DialogConfirmClickListener dialogConfirmClickListener) {
        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(false);
        View view = LayoutInflater.from(context).inflate(R.layout.single_tips_dialog, null);
        dialog.setContentView(view);
        ((TextView) view.findViewById(R.id.tv_true_false_content)).setText(toast);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_tips_dialog_title);
        TextView tvConfirm = (TextView) view.findViewById(R.id.tv_true_false_sure);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        tvConfirm.setText(sureText);
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                dialogConfirmClickListener.confirm();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context) * 7 / 9;
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context) * 7 / 9;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    public interface DialogConfirmClickListener {
        //        public abstract void cancel();
        void confirm();
    }

    /*
   *支付密码对话框
   *
   * showForgetPwd  是否显示“忘记密码”
   */
    public static Dialog showPasswordDialog(Context context, DialogPasswordCompletedListener dialogConfirmClickListener, boolean showForgetPwd) {
        return ShowPasswordDialog(context, "", dialogConfirmClickListener, showForgetPwd);
    }

    public interface DialogPasswordCompletedListener {
        void confirm(String password);//输入密码完毕

        void forgetPwd();//忘记密码
    }

    private static Dialog ShowPasswordDialog(Context context, String toast, final DialogPasswordCompletedListener listener, boolean showForgetPwd) {
        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.pay_password_layout, null);
        dialog.setContentView(view);
        final PasswordView pwdView = (PasswordView) view.findViewById(R.id.pwd_view);
        pwdView.getForgetTextView().setVisibility(showForgetPwd ? View.VISIBLE : View.INVISIBLE);

        //添加密码输入完成的响应
        pwdView.setOnFinishInput(new OnPasswordInputFinish() {
            @Override
            public void inputFinish() {
                //输入完成后我们简单显示一下输入的密码
                //也就是说——>实现你的交易逻辑什么的在这里写
                dialog.dismiss();
                listener.confirm(pwdView.getStrPassword());
            }
        });

        /**
         *  可以用自定义控件中暴露出来的cancelImageView方法，重新提供相应
         *  如果写了，会覆盖我们在自定义控件中提供的响应
         *  可以看到这里toast显示 "Biu Biu Biu"而不是"Cancel"*/
        pwdView.getCancelImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        pwdView.getForgetTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.forgetPwd();
            }
        });

        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context);
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context);
        }
        mWindow.setAttributes(lp);
        mWindow.setGravity(Gravity.BOTTOM);
        dialog.show();
        return dialog;
    }

    /*
     *两个都可点击个确认提示对话框
     */
    public static Dialog showDoubleConfirmDialog(Context context, String title, String content, String sureText, String cancelText, final DialogDoubleConfirmClickListener dialogConfirmClickListener) {
        return ShowDoubleConfirmDialog(context, title, content, sureText, cancelText, dialogConfirmClickListener);
    }

    public interface DialogDoubleConfirmClickListener {
        //        public abstract void cancel();
        void confirm(int type);
    }

    private static Dialog ShowDoubleConfirmDialog(Context context, String title, String toast, String sureText, String cancleText, final DialogDoubleConfirmClickListener dialogConfirmClickListener) {
        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(false);
        View view = LayoutInflater.from(context).inflate(R.layout.tips_dialog, null);
        dialog.setContentView(view);
        ((TextView) view.findViewById(R.id.tv_true_false_content)).setText(toast);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_tips_dialog_title);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_true_false_cancel);
        TextView tvConfirm = (TextView) view.findViewById(R.id.tv_true_false_sure);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        tvCancel.setTextColor(context.getResources().getColor(R.color.font_main));
        tvCancel.setText(cancleText);
        tvConfirm.setText(sureText);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogConfirmClickListener.confirm(0);
                dialog.dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                dialogConfirmClickListener.confirm(1);
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context) * 7 / 9;
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context) * 7 / 9;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    //选择快递公司
    public static Dialog showChooseEms(Context context, List<String> list, EmsItemClickListener emsItemClickListener) {
        return ShowChooseEms(context, list, emsItemClickListener);
    }

    public interface EmsItemClickListener {
        void chooseOther();

        void confirm(int position);

        void cancel();
    }

    private static android.app.Dialog ShowChooseEms(Context context, final List<String> list, final EmsItemClickListener emsItemClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.ems_dialog, null);
        WheelView wva = (WheelView) view.findViewById(R.id.wheel_view);
        TextView tip = (TextView) view.findViewById(R.id.tv_ems_none_tip);
        TextView tvLeft = (TextView) view.findViewById(R.id.tv_choose_company);
        TextView tvRight = (TextView) view.findViewById(R.id.tv_choose_company_confirm);
        if (list.size() == 0) {
            wva.setVisibility(View.GONE);
            tip.setVisibility(View.VISIBLE);
        } else {
            wva.setVisibility(View.VISIBLE);
            tip.setVisibility(View.GONE);
        }
        int index = (int) Math.floor((list.size() / 2.0));
        final int[] currentIndex = {index};
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                emsItemClickListener.chooseOther();
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                emsItemClickListener.cancel();
            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (list.size() > 0) {
                    emsItemClickListener.confirm(currentIndex[0]);
                } else {
                    emsItemClickListener.cancel();
                }
            }
        });
        wva.setOffset(2);
        wva.setItems(list);
        wva.setSeletion(index);
        wva.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                currentIndex[0] = selectedIndex - 2;
            }
        });
        dialog.setContentView(view);
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context) * 8 / 9;
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context) * 8 / 9;
        }
        mWindow.setAttributes(lp);
        mWindow.setWindowAnimations(R.style.Menu_PopuWindow_style);
        dialog.show();
        return dialog;
    }

    /*
        *选择密保问题底部的dialog
        */
    public static android.app.Dialog showQuestionBottomText(Context context, List infos, int index, final MenuDialogOnItemClickListener menuDialogClickListener) {
        return ShowQuestionBottomMenuDialog(context, infos, index, menuDialogClickListener);
    }

    private static android.app.Dialog ShowQuestionBottomMenuDialog(Context context, List<String> infos, int position, final MenuDialogOnItemClickListener menuDialogClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_select_password_question, null);
        WheelView wva = (WheelView) view.findViewById(R.id.wheel_view);
        LinearLayout ler_save = (LinearLayout) view.findViewById(R.id.ler_save);
        LinearLayout ler_back = (LinearLayout) view.findViewById(R.id.ler_back);
        ler_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        int index;
        if (position == -1) {
            index = 0;
        } else {
            index = position;
        }

        final int[] currentIndex = {index};
        ler_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                menuDialogClickListener.onItemClick(currentIndex[0]);
            }
        });
        wva.setOffset(2);
        wva.setItems(infos);
        wva.setSeletion(index);
        wva.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                currentIndex[0] = selectedIndex - 2;
            }
        });
        dialog.setContentView(view);
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context);
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context);
        }
        mWindow.setAttributes(lp);
        mWindow.setGravity(Gravity.BOTTOM);
        mWindow.setWindowAnimations(R.style.Menu_PopuWindow_style);
        dialog.show();
        return dialog;
    }

    /*
   *底部的dialog
   */
    public static android.app.Dialog showBottomMenu(Context context, String[] str, final MenuDialogOnItemClickListener menuDialogClickListener) {
        return ShowBottomMenuDialog(context, str, menuDialogClickListener);
    }

    private static android.app.Dialog ShowBottomMenuDialog(Context context, String[] str, final MenuDialogOnItemClickListener menuDialogClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(true);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_popupwindow_bottommenu, null);
        dialog.setContentView(view);
        TextView menu4 = (TextView) view.findViewById(R.id.pop_5);
        TextView menu3 = (TextView) view.findViewById(R.id.pop_4);
        TextView menu2 = (TextView) view.findViewById(R.id.pop_2);
        TextView menu1 = (TextView) view.findViewById(R.id.pop_1);
        TextView missBtn = (TextView) view.findViewById(R.id.pop_3);
        View divider1 = view.findViewById(R.id.popup_window_bottom_divider1);
        View divider2 = view.findViewById(R.id.popup_window_bottom_divider2);
        View divider3 = view.findViewById(R.id.popup_window_bottom_divider3);
        switch (str.length) {
            case 1:
                menu1.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                break;
            case 2:
                divider1.setVisibility(View.VISIBLE);
                menu1.setVisibility(View.VISIBLE);
                menu2.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                menu2.setText(str[1]);
                break;
            case 3:
                divider1.setVisibility(View.VISIBLE);
                divider2.setVisibility(View.VISIBLE);
                menu1.setVisibility(View.VISIBLE);
                menu2.setVisibility(View.VISIBLE);
                menu3.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                menu2.setText(str[1]);
                menu3.setText(str[2]);
                break;
            case 4:
                divider1.setVisibility(View.VISIBLE);
                divider2.setVisibility(View.VISIBLE);
                divider3.setVisibility(View.VISIBLE);
                menu1.setVisibility(View.VISIBLE);
                menu2.setVisibility(View.VISIBLE);
                menu3.setVisibility(View.VISIBLE);
                menu4.setVisibility(View.VISIBLE);
                menu1.setText(str[0]);
                menu2.setText(str[1]);
                menu3.setText(str[2]);
                menu4.setText(str[3]);
        }
        menu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(1);
                dialog.dismiss();
            }
        });
        menu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(2);
                dialog.dismiss();
            }
        });
        menu3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(3);
                dialog.dismiss();
            }
        });
        menu4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuDialogClickListener.onItemClick(4);
                dialog.dismiss();
            }
        });
        missBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {//横屏
            lp.width = DeviceUtils.getDisplayHeight(context) / 2;
        } else {
            lp.width = DeviceUtils.getDisplayWidth(context);
        }
        mWindow.setAttributes(lp);
        mWindow.setGravity(Gravity.BOTTOM);
        mWindow.setWindowAnimations(R.style.dialog_bottom_in);
        dialog.show();
        return dialog;
    }
}
